package com.ts.view;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class TSurfaceView  implements SurfaceHolder.Callback,OnPreparedListener{
	

    private SurfaceHolder holder; 
    private MediaPlayer player; 
    private String path;
    
    
    

public TSurfaceView(SurfaceView surfaceView,String dataPath) {
		super();
		path = dataPath;
		holder=surfaceView.getHolder();//SurfaceHolder是SurfaceView的控制接口
		holder.addCallback(this); //因为这个类实现了SurfaceHolder.Callback接口，所以回调参数直接this
		holder.setFixedSize(320, 220);//显示的分辨率,不设置为视频默认
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//Surface类型
        player=new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setOnPreparedListener(this);

	}

@Override
public void surfaceCreated(SurfaceHolder holder) {
	//必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
	// 当SurfaceView中的Surface被创建的时候被调用 
    //在这里我们指定MediaPlayer在当前的Surface中进行播放 
    player.setDisplay(holder); 
    //在指定了MediaPlayer播放的容器后，我们就可以使用prepare或者prepareAsync来准备播放了 
    //设置显示视频显示在SurfaceView上
    try {
        player.setDataSource(path);
        player.prepare();
    } catch (Exception e) {
        e.printStackTrace();
    }
	
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
		int height) {
	// TODO Auto-generated method stub
	
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
	// TODO Auto-generated method stub
	
}



@Override
public void onPrepared(MediaPlayer mp) {
	// TODO Auto-generated method stub
	  //然后开始播放视频 
    
    player.start(); 
}




}
